package com.csair.seam.infrastructure.log;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csair.seam.domain.model.entity.SysUser;
import com.csair.seam.domain.model.entity.Visits;
import com.csair.seam.domain.service.ISysUserService;
import com.csair.seam.domain.service.IVisitsService;

@Component
public class VisitsLogEventListener {
	
	@Autowired
	private IVisitsService visitsService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Async
    @EventListener(VisitsLogEvent.class)
	public void handler(VisitsLogEvent event) {
		Visits visits = event.getVisits();
		QueryWrapper<Visits> queryWrapper = new QueryWrapper<>(visits);
		List<Visits> list = visitsService.list(queryWrapper);
		if(CollectionUtils.isEmpty(list)) {
			SysUser sysUser = sysUserService.queryUserByAccount(visits.getUserCode());
			visits.setOrgId(sysUser.getOrgId());
			visitsService.save(visits);
		}else {
			Visits updateVisits = list.get(0);
			visitsService.addVisitNum(updateVisits);
		}
	}
}
